/**
 * Created by 联想 on 2017/5/26.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class SocketService {
    public static void main(String[] args) throws IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
        SocketService socketService = new SocketService();
        try {
            socketService.oneServer();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
    }
    public  void oneServer() throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        MyDC mydc=new MyDC();

        try{
            ServerSocket server=null;
            try{
                server=new ServerSocket(5209);
                System.out.println("服务器启动成功");
            }catch(Exception e) {
                System.out.println("没有启动监听："+e);
            }
            Socket socket=null;
            try{
                socket=server.accept();
            }catch(Exception e) {
                System.out.println("Error."+e);
            }
            String line;
            BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter writer=new PrintWriter(socket.getOutputStream());

            line=in.readLine();
            String line1=in.readLine();
            System.out.printf("密文=%s\n",line);
            // 获取密钥
            byte[]ctext=line.getBytes("ISO-8859-1");
            FileInputStream  f2=new FileInputStream("keykb1.dat");
            int num2=f2.available();
            byte[ ] keykb=new byte[num2];
            System.out.printf("\n");
            f2.read(keykb);
            SecretKeySpec k=new  SecretKeySpec(keykb,"DESede");
            // 解密
            Cipher cp=Cipher.getInstance("DESede");
            cp.init(Cipher.DECRYPT_MODE, k);
            byte []ptext=cp.doFinal(ctext);
            // 显示明文

            String p=new String(ptext,"UTF8");

            int a;


            System.out.println("明文:"+p);
            String Np="";
            for(int i=1;i<p.length();i++)
                Np+=p.charAt(i);
            a=mydc.evaluate(Np);
            String x=p;
            MessageDigest m=MessageDigest.getInstance("MD5");
            m.update(x.getBytes("UTF8"));
            byte s[ ]=m.digest( );
            String result="";
            for (int i=0; i<s.length; i++){
                result+=Integer.toHexString((0x000000ff & s[i]) |
                        0xffffff00).substring(6);
            }
            if(!(line1.equals(result)))System.out.printf("MD5比对正确！\n");

            writer.println(a);
            writer.flush();

            writer.close();
            in.close();
            socket.close();
            server.close();
        }catch(Exception e) {
            System.out.println("Error."+e);
        }
        // 获取密文

    }

}